from fontTools.ttLib import TTFont

font = TTFont("./file.ttf")

cmap = font.get("cmap").getBestCmap()
# print(cmap)

ttf_dict = dict()

for k, v in cmap.items():
    # print(k, v)  # 原样数据
    # print(hex(k), v)  # 转换为十六进制
    if v[3:]:
        # 1. 提取
        # print(hex(k), v)
        # print(hex(k), v[3:])
        # print(hex(k), "00" + v[3:] if len(v[3:]) == 2 else v[3:])
        # print(hex(k), "\\u00" + v[3:] if len(v[3:]) == 2 else "\\u" + v[3:])

        # 2. 转换为真正的字
        content = "\\u00" + v[3:] if len(v[3:]) == 2 else "\\u" + v[3:]
        real_content = content.encode('utf-8').decode('unicode_escape')
        # print(hex(k), real_content)

        # 3. 替换k
        k_hex = hex(k)
        real_k = k_hex.replace("0x", "&#x")
        # print(real_k, real_content)

        # 4. 封装为字典
        ttf_dict[real_k] = real_content

print("解析后：", ttf_dict)
